import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.border.EmptyBorder;


@SuppressWarnings("serial")
public class Calculator extends JFrame implements ActionListener{

    private JPanel jp;
    private JTextField jtf1;
    private JTextField jtf2;
    private JTextField jtf3;
    private JTextField jtf5;
    
    // private JTextField jtf;
    // private JTextField jtf1;
    // private JTextField jtf2;
    // private JButton jb;
	public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Calculator frame = new Calculator();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
	
	public Calculator() {
		setTitle("Calculator");
		setSize(300, 100);
        jp = new JPanel();
        
        jp.setLayout(new GridLayout(2,5));
        setContentPane(jp);

        JPanel jp1 = new JPanel();
        
        jtf1 = new JTextField("12", 5);
        jtf2 = new JTextField(" ", 5);
        jtf3 = new JTextField("7", 5);
        JTextField jtf4 = new JTextField("  =", 5);
        jtf5 = new JTextField(5);
        jp1.add(jtf1);
        jp1.add(jtf2);
        jp1.add(jtf3);
        jp1.add(jtf4);
        jp1.add(jtf5);


        JPanel jp2 = new JPanel();
        
        JButton jb1 = new JButton("+");
        JButton jb2 = new JButton("-");
        JButton jb3 = new JButton("*");
        JButton jb4 = new JButton("/");
        JButton jb5 = new JButton("OK");
        jb1.addActionListener(this);
        jb2.addActionListener(this);
        jb3.addActionListener(this);
        jb4.addActionListener(this);
        jb5.addActionListener(this);
        
        jp2.add(jb1);
        jp2.add(jb2);
        jp2.add(jb3);
        jp2.add(jb4);
        jp2.add(jb5);

        
        jp.add(jp1);
        jp.add(jp2);
	}
	
	public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        String cmd = arg0.getActionCommand();
        String result = "";
        double a = Double.parseDouble(jtf1.getText());
        double b = Double.parseDouble(jtf3.getText());
        if(cmd.equals("+")){
            jtf2.setText("  +");
        }
        else if(cmd.equals("-")){
            jtf2.setText("  -");
        }
        else if(cmd.equals("  *")){
            jtf2.setText("  *");
        }
        else if(cmd.equals("/")){
            jtf2.setText("  /");
        }

        if(cmd.equals("OK")){
            String op = jtf2.getText();
            if(op.equals("  +")){
                result = "" + (a + b);
            } 
            else if(op.equals("  -")){
                result = "" + (a - b);
            }
            else if(op.equals("  *")){
                result = "" + (a * b);
            }
            else if(op.equals("  /")){
                result = "" + (a / b);
            }
        }
        jtf5.setText(result);
	}

}
